package per.zqx.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import per.zqx.utils.RabbitUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * Producer
 *
 * @author Sa  2021-06-02 17:48
 */
public class Producer {

    static final String EXCHANGE_NAME = "routing_exchange";
    static final String ROUTER_KEY_1 = "router_key_1";
    static final String ROUTER_KEY_2 = "router_key_2";
    static final String ROUTER_KEY_3 = "router_key_3";

    public static void main(String[] args) throws IOException, TimeoutException {
        try (Connection connection = RabbitUtils.getConnection()){
            Channel channel = connection.createChannel();
            // 交换机分发模式：direct模式
            channel.exchangeDeclare(EXCHANGE_NAME,"direct");
            String msg = "路由分发模式测试";
            // 发送给交换机时，指定路由键
            channel.basicPublish(EXCHANGE_NAME,ROUTER_KEY_2,null,msg.getBytes(StandardCharsets.UTF_8));
            System.out.println("Send message -> " + msg);
        }
    }
}
